{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1的值为：\n",
      " [[ 1.46699641 -1.61465376  1.09966574]\n",
      " [-0.17921545 -0.29345533 -0.09559221]\n",
      " [-1.28277725 -1.40459208 -0.70275324]\n",
      " [-1.47449565  1.06338275  0.61658503]]\n",
      "筛选小于1的数值：\n",
      " [[False  True False]\n",
      " [ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True False  True]]\n",
      "arr2的值为：\n",
      " [[ 0.03713871 -0.45175494 -1.86242767]\n",
      " [ 1.15499122 -1.25759422  0.6305903 ]\n",
      " [ 0.15195863  0.29894668  1.19767465]\n",
      " [ 0.10450922  0.28813936  0.23021893]]\n",
      "筛选arr1 > arr2的值：\n",
      " [[ True False  True]\n",
      " [False  True False]\n",
      " [False False False]\n",
      " [False  True  True]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.randn(4, 3)\n",
    "print(\"arr1的值为：\\n\", arr1)\n",
    "print(\"筛选小于1的数值：\\n\", arr1 < 1)\n",
    "arr2 = np.random.randn(4, 3)\n",
    "print(\"arr2的值为：\\n\", arr2)\n",
    "print(\"筛选arr1 > arr2的值：\\n\", arr1 > arr2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 逻辑运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1_bool1的数值为：\n",
      " [[ True False  True]\n",
      " [ True  True  True]\n",
      " [False False False]\n",
      " [False  True  True]]\n",
      "arr1_bool2的数值为：\n",
      " [[False  True False]\n",
      " [ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True False False]]\n",
      "逻辑与：\n",
      " [[False False False]\n",
      " [ True  True  True]\n",
      " [False False False]\n",
      " [False False False]]\n",
      "逻辑或：\n",
      " [[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]]\n",
      "逻辑非：\n",
      " [[False  True False]\n",
      " [False False False]\n",
      " [ True  True  True]\n",
      " [ True False False]]\n"
     ]
    }
   ],
   "source": [
    "arr1_bool1 = arr1 > -0.5\n",
    "print(\"arr1_bool1的数值为：\\n\", arr1_bool1)\n",
    "arr1_bool2 = arr1 < 0.5\n",
    "print(\"arr1_bool2的数值为：\\n\", arr1_bool2)\n",
    "print(\"逻辑与：\\n\", arr1_bool1 & arr1_bool2)\n",
    "print(\"逻辑或：\\n\", arr1_bool1 | arr1_bool2)\n",
    "print(\"逻辑非：\\n\", ~arr1_bool1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 二元通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "二元通用与函数：\n",
      " [[False False False]\n",
      " [ True  True  True]\n",
      " [False False False]\n",
      " [False False False]]\n",
      "二元通用或函数：\n",
      " [[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]]\n",
      "二元通用非函数：\n",
      " [[False  True False]\n",
      " [False False False]\n",
      " [ True  True  True]\n",
      " [ True False False]]\n"
     ]
    }
   ],
   "source": [
    "print(\"二元通用与函数：\\n\", np.logical_and(arr1_bool1, arr1_bool2))\n",
    "print(\"二元通用或函数：\\n\", np.logical_or(arr1_bool1, arr1_bool2))\n",
    "print(\"二元通用非函数：\\n\", np.logical_not(arr1_bool1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. np.where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1, 2], [3, 4]])\n",
    "arr2 = np.array([[9, 8], [7, 6]])\n",
    "\n",
    "np.where([], )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑操作\n",
    "你是一名气象学家，需要分析一天中不同时间段的气温数据。给定数组 temps = np.array([[22, 19, 20], [24, 21, 23], [25, 26, 27]]) 表示一天中三个不同地点的温度(单位:摄氏度)。请使用逻辑操作找出所有高于 23 度的温度，并打印出这些温度的具体数值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "高于23度的温度是： [24 25 26 27]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "temps = np.array([[22, 19, 20], [24, 21, 23], [25, 26, 27]])\n",
    "warm_temps = temps[temps > 23]\n",
    "print(\"高于23度的温度是：\", warm_temps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.where\n",
    "在一个游戏中，玩家的生命值 health 和魔法值 mana 被存储在两个数组中。给定 health = np.array([80, 50, 30]) 和 mana = np.array([100, 60, 40])。请使用 np.where 函数创建一个新数组，如果玩家的生命值大于 60，则使用生命值，否则使用魔法值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[80 60 40]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "health = np.array([80, 50, 30])\n",
    "mana = np.array([100, 60, 40])\n",
    "player_status = np.where(health>60, health, mana)\n",
    "print(player_status)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 逻辑操作\n",
    "你是一名数据分析师，正在研究一个城市的降雨数据。给定数组 rainfall = np.array([[0.2, 0, 0.1], [0, 0.3, 0], [0.4, 0, 0.5]]) 表示一周内每天的降雨量(单位:米)。请使用逻辑操作找出所有降雨量大于 0.2 米的天数，并打印出这些天的降雨量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([1, 2, 2], dtype=int64), array([1, 0, 2], dtype=int64))\n",
      "[0.3 0.4 0.5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "rainfall = np.array([[0.2, 0, 0.1], [0, 0.3, 0], [0.4, 0, 0.5]])\n",
    "heavy_day = np.where(rainfall > 0.2)\n",
    "print(heavy_day)\n",
    "heavy_rain = rainfall[rainfall > 0.2]\n",
    "print(heavy_rain)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.where\n",
    "在一个农场游戏中，你要根据作物的生长情况决定是否浇水。给定数组 crop_health = np.array([[1, 0, 1], [1, 1, 0], [0, 1, 1]]) 表示作物的健康状况，其中 1 表示健康，0 表示需要浇水。请使用 np.where 函数创建一个新数组，如果作物健康，则分配值 1，否则分配值 -1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1 -1  1]\n",
      " [ 1  1 -1]\n",
      " [-1  1  1]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "crop_health = np.array([[1, 0, 1], [1, 1, 0], [0, 1, 1]])\n",
    "water_needed = np.where(crop_health==1, 1, -1)\n",
    "print(water_needed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
